import debugCallBacks
from whiff import resolver
from whiff import whiffenv
from whiff.rdjson import jsonParse
from whiff.middleware import callTemplate
import types

def erdDelete(env, start_response):
    env = resolver.process_cgi(env, parse_cgi=True)
    name = whiffenv.cgiGet(env, "name")
    rule = whiffenv.cgiGet(env, "Rule")
    rname = whiffenv.cgiGet(env, "rname")
    weakStr = whiffenv.cgiGet(env, "weak")
    (flag, weak, cursor) = jsonParse.parseValue(weakStr)
    assert flag, "failed to parse weak argument "+repr(weakStr)
    #pr "adding participation (name, rname, weak)", (name, rname, weak)
    minimum = whiffenv.cgiGet(env, "minimum")
    maximum = whiffenv.cgiGet(env, "maximum")
    #pr "min/max", minimum, maximum
    if len(minimum)==1:
        minimum = int(minimum)
    if len(maximum)==1:
        maximum = int(maximum)
    #pr "key (name, rname,rule)", (name, rname,rule)
    rule = resolver.unquote(rule)
    name = name.strip()
    (flag, rulevalue, cursor) = jsonParse.parseValue(rule)
    if not flag:
        raise ValueError, "failed to parse rule "+repr(rule)
    if not name:
        raise ValueError, "no name found"
    # check for existence of entity and relationship
    efound = rfound = False
    newrule = []
    #pr "old rule is", jsonParse.format(rulevalue)
    for info in rulevalue:
        newrule.append(info)
        indicator = info[0]
        iname = info[1]
        if indicator=="entity":
            if iname==name:
                efound = True
                if weak:
                    (indicator, iname, ix, iy, iweak) = info
                    assert not iweak, "entity is already marked weak, cannot reset defining relationship "+repr(name)
                    # edit the entry to mark entity as weak
                    newrule[-1] = (indicator, iname, ix, iy, True)
        elif indicator=="relationship":
            if iname==rname:
                rfound = True
                if weak:
                    (indicator, iname, ix, iy, iweak) = info
                    assert not iweak, "relationship is already marked weak, cannot reuse it to define another entity "+repr(rname)
                    # edit the entry to mark entity as weak
                    newrule[-1] = (indicator, iname, ix, iy, True)
    assert rfound, "no such relationship found "+repr(rname)
    assert efound, "no such entity found "+repr(name)
    rulevalue = newrule
    if weak:
        minimum = maximum = 1
    #start_response('200 OK', [('Content-Type', 'application/javascript')])
    #yield "alert('trying to delete element %s');" % name
    #yield "alert('rule is %s');" % rule.replace("\n", " ")
    # remove all mentions of element
    newrule = list(rulevalue)
    newrule.append( ["participate", name, rname, "%s_%s_%s"%(name, rname, len(newrule)), minimum, maximum] )
    env = env.copy()
    newrulestr = jsonParse.format(newrule)
    #pr "new rule is"
    #pr newrulestr
    resolver.override_cgi_require(env, "Rule", newrulestr)
    # deliver the javascript
    #pr "NOW CALLING ERDTEST"
    app = callTemplate.callTemplate("erdTest.js")
    return app(env, start_response)

__wsgi__ = erdDelete
